<template>
	<view class="addEditForm">
		<view class="data-form bgfff">
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">类型</text>
				<view class="data-form-input">
					<cpdd-select v-model="dataForm.parentType" :disabled="optionDisabled" :list="selectList" keyname="name"
						valname="val"></cpdd-select>
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">子类型名称</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.typeName"
						:placeholder="optionDisabled?'-':'请输入标题'" placeholder-class="plStyle" />
				</view>
			</view>
		</view>
		<view class="right-area-footer2 flex-ac bgfff" v-if="!optionDisabled">
			<view class="saveBtn cancleBtn" @tap="handleCancle">取消</view>
			<view class="saveBtn" @tap="handleSubmit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				did: '',
				dataForm: { //表单
					id: '',
					typeName: '',
					parentType: ''
				},
				cpddDateRangeVal: '',
				cpddDateRangeVal2: '',
				selectList: [{
						name: '食品',
						val: 'Food'
					},
					{
						name: '食用农产品',
						val: 'FoodFarm'
					},
					{
						name: '食品添加剂',
						val: 'FoodAdd'
					},
					{
						name: '食品相关',
						val: 'FoodRelate'
					}
				], //上一页带过来，如果上一页用不到，可以在这里调用赋值
				optionDisabled: false,
			}
		},
		onLoad(options) {
			console.log('options', options);
			this.did = options.id || ''
			this.optionDisabled = options.op == 'look' ? true : false
			let title = options.op == 'look' ? '详情' : options.op == 'add' ? '新增' : '编辑'
			uni.setNavigationBarTitle({
				title
			})
			if (options.op != 'add') {
				this.loadDetail();
			}
		},
		methods: {
			loadDetail() {
				this.$http.get(`/material/foodType/${this.did}`).then(res => {
					if (res.code == 200) {
						Object.keys(this.dataForm).forEach(key => this.dataForm[key] = res.data[key])
					}
				})
			},
			handleCancle() {
				this.$u.back()
			},
			handleSubmit() {
				console.log('提交');
				if (!this.dataForm.parentType) return this.$u.msg("请选择类型")
				if (!this.dataForm.typeName) return this.$u.msg("请输入标题")
				this.realSumbit();
			},
			realSumbit() {
				if (this.dataForm.id) {
					this.$http.put('/material/foodType', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('修改成功')
							this.$u.back(1, 1000)
						}
					})
				} else {
					this.$http.post('/material/foodType', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('已新增')
							this.$u.back(1, 1000)
						}
					})
				}
			}

		}
	}
</script>

<style>

</style>